.pragma library



function DCDataClasses() {
    this.isError = false
    this.errorString = ""
    this.statusString = ""
    this.username = ""
    this.password = ""
    this.karpoid = ""
    this.newmessagesavailable = false
}


function DCPersonalProfile() {
    DCDataClasses.call(this)

    this.confirmpassword = ""
    this.deleteaccount = false
    this.firstname = ""
    this.lastname = ""
    this.hometown = ""
    this.phone = ""
    this.alias = ""
    this.showname = false
    this.showphone = false
    this.showhometown = false
}

DCPersonalProfile.prototype = new DCDataClasses;

function DCUserList() {
    DCDataClasses.call(this)

    this.phoneRequired = false
    this.firstnameRequired = false
    this.lastnameRequired = false
    this.hometownRequired = false
    this.userlist = []
}

DCUserList.prototype = new DCDataClasses;

function DCCarpoolMember() {
    this.carpoolmemberkarpoid = ""
    this.carpoolkarpoid = ""
    this.ownerkarpoid = ""
    this.memberkarpoid = ""
    this.firstname = ""
    this.lastname = ""
    this.phone = ""
    this.hometown = ""
    this.status = ""
    this.picklat = ""
    this.picklon = ""

}


function DCCarpool() {
    this.carpoolkarpoid = ""
    this.ownerkarpoid = ""
    this.name = ""
    this.description = ""
    this.routekarpoid = ""
    this.routename = ""
    this.members = []
}


function DCCarpoolList() {
    DCDataClasses.call(this)

    this.carpoollist = []

}

DCCarpoolList.prototype = new DCDataClasses;

function DCCarpoolMembersList() {
    DCDataClasses.call(this)

    this.members = []

}

DCCarpoolMembersList.prototype = new DCDataClasses;


function DCKarpoIdList() {
    DCDataClasses.call(this)

    this.karpoids = []

}

DCKarpoIdList.prototype = new DCDataClasses;

function DCMessageOut() {
    DCDataClasses.call(this)

    this.receiverskarpoidarray = []
    this.message = ""
    this.statuschangetime = ""
}

DCMessageOut.prototype = new DCDataClasses;


function DCMessage() {
    this.messagekarpoid = ""
    this.senderkarpoid = ""
    this.receiverkarpoid = ""
    this.message = ""
    this.statuschangetime = ""
    this.status = ""
    this.senderusername = ""
    this.receiverusername = ""

}

function DCMessageList() {
    DCDataClasses.call(this)

    this.condition = "" // NEW, ALL, SENT
    this.messagelist = []
}

DCMessageList.prototype = new DCDataClasses;

function DCCarpoolActivation() {
    this.carpoolkarpoid = ""
    this.active = "0"
    this.alias = ""
    this.lat = ""
    this.lon = ""
}

function DCCarpoolActivationsList() {
    DCDataClasses.call(this)

    this.carpoolactivationlist = []
}

DCCarpoolActivationsList.prototype = new DCDataClasses;


function DCMyTrip() {
    this.memberindexkarpoid = ""
    this.carpoolkarpoid = ""
    this.picklat = ""
    this.picklon = ""
    this.routeindexkarpoid = ""
    this.ownerkarpoid = ""
    this.ownername = ""
    this.ownerphone = ""
    this.ownerhometown = ""
    this.owneralias = ""
    this.routename = ""
    this.routedescription = ""
    this.routetime = ""
    this.routerepeat = ""
    this.routefromstreet = ""
    this.routefromcity = ""
    this.routefrompostcode = ""
    this.routefromcountry = ""
    this.routefromlat = ""
    this.routefromlon = ""
    this.routetostreet = ""
    this.routetocity = ""
    this.routetopostcode = ""
    this.routetocountry = ""
    this.routetolat = ""
    this.routetolon = ""
    this.active = ""
}

function DCMyTripList() {
    DCDataClasses.call(this)

    this.mytriplist = []
}

DCMyTripList.prototype = new DCDataClasses;


function DCRoutePoint() {
    this.type = ""
    this.pointset =  false
    this.latitude =  0.0
    this.longitude =  0.0
    this.country =  ""
    this.countryCode =  ""
    this.state =  ""
    this.city =  ""
    this.district =  ""
    this.street =  ""
    this.postcode =  ""
    this.distance =  0
    this.traveltime =  0
    this.routepolyline =  ""
}

function DCRoute() {
    DCDataClasses.call(this)

    this.routekarpoid = ""
    this.ownerkarpoid = ""
    this.name = ""
    this.description = ""
    this.time = ""
    this.repeat = ""
    this.route = [] // DCRoutePoint
    this.lat1 = 0.0
    this.lon1 = 0.0
    this.lat2 = 0.0
    this.lon2 = 0.0
    this.showownername = ""
    this.showownerhometown = ""
    this.showownerphone = ""
    this.carpoolkarpoid = ""
    this.ownerinfo = "" // DCPersonalProfile
}

DCRoute.prototype = new DCDataClasses;

function DCRouteList() {
    DCDataClasses.call(this)

    this.routelist = [] // DCRoute
    this.fromorto = ""
    this.lat1 = 0.0
    this.lon1 = 0.0
    this.lat2 = 0.0
    this.lon2 = 0.0
}

DCRouteList.prototype = new DCDataClasses;


function DCJoinRequest() {
    DCDataClasses.call(this)

    this.joinrequestkarpoid = ""
    this.carpoolkarpoid = ""
    this.routekarpoid = ""
    this.ownerkarpoid = ""
    this.travellerkarpoid = ""
    this.showname = ""
    this.showphone = ""
    this.showhometowns = ""
    this.message = ""
    this.picklat = 0.0
    this.picklon = 0.0
}

DCJoinRequest.prototype = new DCDataClasses;
// ------

function send(dcclass, datacloudurl, action, responsecb)
{
    try
    {
        var jsonstring = JSON.stringify(dcclass)
        var data = "action=" + action + "&json=" + encodeURIComponent(jsonstring);

        console.log("JSON:\r\n" + jsonstring + "\r\n----------------------\r\n")

        var xmlhttp = new XMLHttpRequest();

        xmlhttp.open("POST",datacloudurl+"/KarpoDC",true);

        xmlhttp.onreadystatechange = function() {

            if (xmlhttp.readyState == XMLHttpRequest.DONE)
            {
                try
                {
                    var result = JSON.parse(decodeURIComponent(xmlhttp.responseText))
                    try
                    {
                        notifynewmessages(result.newmessagesavailable)
                    }
                    catch (e) {}

                    responsecb(result)
                }
                catch (e)
                {
                    console.log("SEND EXECEPTION: " + e + "\r\nRESSPONSETEXT: " + xmlhttp.responseText)
                }
            }
        }

        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-Length", data.length);


        xmlhttp.send(data);
    }
    catch (e)
    {
        console.log("EXCEPTION:\r\n" + e);
    }
}

// ------


var messagepollingenabled = true
var messagepollingcb = null
var messagecbarr = []

function appendmessagecb(messagecb)
{
    messagecbarr[messagecbarr.length] = messagecb
}

function notifynewmessages(newmessages)
{
    for (var i=0; i < messagecbarr.length; i++)
    {
        messagecbarr[i](newmessages)
    }
}


function getTimestamp()
{
    var starttime = new Date(0);
    var now       = new Date();
    var timestamp = Math.round((now-starttime)/1000); // seconds since 1.1.1970

    return timestamp;
}


function secondToHMS(travtime)
{
    var hours = travtime / 3600;
    hours = parseInt(hours.toString())
    var minutes = (travtime - hours*3600)/60
    minutes = parseInt(minutes.toString())
    var seconds = travtime - hours*3600 - minutes*60
    var travtimestr = ""
    if (hours > 0)
    {
        travtimestr = hours.toString() + " h "
    }

    if (minutes > 0)
    {
        travtimestr += minutes.toString() + " min "
    }

    if (seconds > 0)
    {
        travtimestr += seconds.toString() + " sec "
    }

    return travtimestr;
}


